package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.blade.msg.DataMessage;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.Dictionary;
import org.smr.ministore.entities.DictionaryGroupQueryVO;
import org.smr.ministore.entities.vo.DictionaryQueryVO;
import org.smr.ministore.service.DictionaryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller("DictionaryController")
public class DictionaryController {

/*
	@Resource
	private FeignDictionaryMicroServiceClient service;
*/

    @Resource
    private DictionaryService service;


    /**
	 * 保存编辑后的Dictionary信息ß
	 * @param Dictionary
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/Dictionary/edit")
	public Message modify(@RequestBody Dictionary Dictionary){
		Message msg = new Message();
		
		if(null == Dictionary.getId()){
			msg.setStatus(service.save(Dictionary));
		}else{
			msg.setStatus(service.updateById(Dictionary));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询Dictionary
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/Dictionary/list-page")
	public String toList(){
		return "/Dictionary/Dictionary-list";
	}

	/**
	*  根据id查询Dictionary
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/Dictionary")
	public Dictionary get( Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询Dictionary信息
	 * @param Dictionary
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Dictionary/list")
	public Pager<Dictionary> list(@RequestBody Dictionary Dictionary , long pageSize, long pageNum ){

		return service.paginate(Dictionary, pageSize, pageNum);

	}
	
	
	/**
	 * 根据ID删除Dictionary信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Dictionary/remove",method = RequestMethod.DELETE)
	public Message remove( Long id){
		
		Message msg = new Message();
		msg.setStatus(service.deleteById(id));
		return msg;
	}

    /**
     * 获取字典组
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping( value = "/Dictionary/getDictionaryGroup",method = RequestMethod.POST)
    public List<Dictionary> getDictionaryGroup(@RequestBody DictionaryGroupQueryVO query){

	    return this.service.getDictionaryGroup(query);
    }


	/**
	 * 获取已经引用字典组
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/Dictionary/getReferencedDictionaryList",method = RequestMethod.POST)
	public List<Map<String, Object>> getReferencedDictionaryList(@RequestBody Map params){

	    int sys = MapUtils.getIntValue(params, "sys");

	    Long organId =  MapUtils.getLong(params, "organId");

		Long originId =  MapUtils.getLong(params, "originId");

		String bigType =  MapUtils.getString(params, "bigType");

		List<Map<String, Object>> dictionaryList = this.service.getReferencedDictionaryList(params);


		for(Map dictionary : dictionaryList){

			String id = MapUtils.getString(dictionary, "id");
			dictionary.put("id", id);

			String organIdString = MapUtils.getString(dictionary, "organId");
			dictionary.put("organId", organIdString);

			String parentId = MapUtils.getString(dictionary, "parentId");
			dictionary.put("parentId", parentId);

		}
		return dictionaryList;
	}

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getOrganReferencedDictionaryPagination/{pageSize}/{pageNum}")
    public Pager<Dictionary> getOrganReferencedDictionaryPagination(@RequestBody Dictionary entity,
                                                             @PathVariable("pageSize")Long pageSize,
                                                             @PathVariable("pageNum")Long pageNum ) {

        return service.getOrganReferencedDictionaryPagination(entity, pageSize, pageNum);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/Dictionary/getOrganReferencedDictionary")
    public Message getOrganReferencedDictionary(DictionaryQueryVO query){

	    Dictionary dictionary = new Dictionary();
	    dictionary.setOrganId(query.getOrganId());
	    dictionary.setId(query.getId());
	    dictionary.setBigType(query.getBitType());
        DataMessage<List<Dictionary>> message = new DataMessage<>();
        message.setData(service.getOrganReferencedDictionary(dictionary));
        return message;
	}

}
